package com.sls.game.parser.sls;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.sls.game.pojo.SLSHttpInterfaceInfo;

/**
 * 
 * @description （sls订单状态推送给下游用户返回内容）解析
 * @author Wangxuegang
 * @date 2012-12-13
 */
public class OrderStatusSendParse {
	private static Logger log = Logger.getLogger(OrderStatusSendParse.class);

	public static SLSHttpInterfaceInfo readUserSendMsg(String xmlStr) {
		SLSHttpInterfaceInfo info = new SLSHttpInterfaceInfo();
		// String ptOrderNo = "";
		// String supOrderNo = "";
		String handleResult = "";
		String failedCode = "";
		String failedReason = "";

		InputSource source = null;
		StringReader read = null;
		try {
			// 得到DOM解析器的工厂实例
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			// 从DOM工厂中获得DOM解析器
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
			read = new StringReader(xmlStr);
			source = new InputSource(read);

			// 把要解析的xml文档读入DOM解析器
			Document doc = dbBuilder.parse(source);
			Element root = doc.getDocumentElement();
			NodeList childNodes = root.getChildNodes(); // 获得根元素下的子节点
			// 遍历子节点
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i); // 对每个子节点进行判断
				// if ("ptOrderNo".equals(node.getNodeName())) { // 第二层节点
				// ptOrderNo = node.getTextContent();
				// info.setPtOrderNo(ptOrderNo);
				// } else if ("supOrderNo".equals(node.getNodeName())) {
				// supOrderNo = node.getTextContent();
				// info.setSupOrderNo(supOrderNo);
				// } else

				if ("handleResult".equals(node.getNodeName())) {
					handleResult = node.getTextContent();
					info.setHandleResult(handleResult);
				} else if ("failedCode".equals(node.getNodeName())) {
					failedCode = node.getTextContent();
					info.setFailedCode(failedCode);
				} else if ("failedReason".equals(node.getNodeName())) {
					failedReason = node.getTextContent();
					info.setFailedReason(failedReason);
				}
			}

			return info;
		} catch (Exception e) {
			log.error(e);
		}

		return null;
	}

}
